﻿Imports Microsoft.VisualBasic
Imports System.Security.Cryptography
Imports System.IO

Public Class Security
    Private Shared key() As Byte = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, _
  15, 16, 17, 18, 19, 20, 21, 22, 23, 24}
    Private Shared iv() As Byte = {8, 7, 6, 5, 4, 3, 2, 1}

    Public Shared Function Encrypt(ByVal txt As String) As String
        Dim des As New TripleDESCryptoServiceProvider()
        Dim outb() As Byte = Transform(UnicodeEncoding.UTF8.GetBytes(txt), des.CreateEncryptor(key, iv))

        Return Convert.ToBase64String(outb)
    End Function

    Public Shared Function Decrypt(ByVal txt As String) As String
        Dim des As New TripleDESCryptoServiceProvider()
        Dim outb() As Byte = Transform(Convert.FromBase64String(txt), des.CreateDecryptor(key, iv))

        Return UnicodeEncoding.UTF8.GetString(outb)
    End Function
    Private Shared Function Transform(ByVal input() As Byte, ByVal CryptoTransform As ICryptoTransform) As Byte()
        Dim memStream As MemoryStream = New MemoryStream
        Dim cryptStream As CryptoStream = New  _
            CryptoStream(memStream, CryptoTransform, _
            CryptoStreamMode.Write)

        cryptStream.Write(input, 0, input.Length)
        cryptStream.FlushFinalBlock()
        memStream.Position = 0
        Dim result(CType(memStream.Length - 1, System.Int32)) As Byte
        memStream.Read(result, 0, CType(result.Length, System.Int32))
        memStream.Close()
        cryptStream.Close()
        Return result
    End Function
End Class
